package com.bailan.foreat.controller;


import com.bailan.foreat.entity.Product;
import com.bailan.foreat.service.ProductService;
import com.bailan.foreat.vo.EsProduct;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ste
 * @since 2023-02-21 07:50:43
 */
@Controller
@RequestMapping("/product")
public class ProductController {


    @Autowired
    ProductService productService;


    //旧版，更换为下方select
/*    @RequestMapping("select")
    @ResponseBody
    public Page select(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "9") Integer pageSize,
            Integer storeId,
            String keyword
    ) {
        System.out.println(keyword);
        Page productPage;
        if ("".equals(keyword)) {
            productPage = productService.selectAll(currentPage, pageSize, storeId);
        } else {
            Page<EsProduct> esProductPage = productService.searchByES(currentPage, pageSize, keyword, storeId);
            productPage = esProductPage;
        }
        return productPage;
    }*/


    @RequestMapping("select")
    @ResponseBody
    public Page select(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "6") Integer pageSize,
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "0") Integer typeIdKeyword,
            Integer storeId
    ) {
        Page productPage ;
//        if ("".equals(keyword) && typeIdKeyword == 0) {
//            productPage = productService.selectAll(currentPage, pageSize, storeId);
//        } else {
            Page<EsProduct> esProductPage = productService.searchByES(currentPage, pageSize, keyword,typeIdKeyword, storeId);
            productPage = esProductPage;
//        }
        return productPage;
    }

    @RequestMapping("toProductDetail")
    public String toProductDetail(Integer productId, Model model) {
        model.addAttribute("productId", productId);
        return "productdetail";
    }

    @RequestMapping("queryProductDetail")
    @ResponseBody
    public Product queryProductDetail(Integer productId) {
        return productService.selectById(productId);
    }





}

